<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>

      <el-breadcrumb-item>面经管理</el-breadcrumb-item>

    </el-breadcrumb>

    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>

          <el-button icon="el-icon-plus" size="small" type="primary" round @click="openDrawer('add')">
            添加面经
          </el-button>

        </div>

      </template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="stem" label="标题">
        </el-table-column>

        <el-table-column prop="creator" label="作者">
        </el-table-column>

        <el-table-column prop="likeCount" label="点赞">
        </el-table-column>

        <el-table-column prop="views" label="浏览数">
        </el-table-column>

        <el-table-column prop="createdAt" label="更新时间">

        </el-table-column>

        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <div class="actions">
              <!-- {{ obj }} -->
              <i class="el-icon-view" @click="openDrawer('preview', row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>

          </template>

        </el-table-column>

      </el-table>

      <div class="block">
        <span class="demonstration">完整功能</span>
        <el-pagination background @current-change="handleCurrentChange" :current-page="current" :page-size="pageSize"
          layout="prev, pager, next" :total="total">
        </el-pagination>
      </div>

    </el-card>

    <el-drawer :title=title[drawerType] size="60%" :visible.sync="isShow" direction="rtl" :before-close="handleClose">
      <div v-if="drawerType === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>

      <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="标题" prop="stem">
          <el-input placeholder="请输入面经标题" v-model="form.stem"></el-input>
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <!-- //💕手动校验代码的解读: 当失去焦点的时候,使用Vue里面的form组件里面的validateField方法,来进行手动校验content字段 -->
          <quill-editor @blur="$refs.form.validateField('content')" v-model="form.content"></quill-editor>
        </el-form-item>

        <el-form-item>
          <el-button @click="submit" type="primary">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

  </div>

</template>

<script>
import { addArticle, delArticle, getArticle, getArticles, updateArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      current: 1,
      pageSize: 10,
      tableData: [],
      total: 0,
      isShow: false,
      drawerType: 'add',
      title: {
        add: '添加面经',
        edit: '编辑面经',
        preview: '预览面经'
      },
      form: {
        stem: '',
        content: ''

      },
      rules: {
        stem: [
          // 一个对象表示一条规则
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      const res = await getArticles({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res.data.rows)
      this.tableData = res.data.rows
      this.total = res.data.total
      // console.log(this.total)
    },
    async del (id) {
      // 只要是发请求,就要加上await,不要觉得不拿结果,就不需要加,但是这里的逻辑是有顺序的
      await delArticle(id)
      // 提示用户
      this.$message.success('删除成功')
      // 渲染之前先判断,如果当前只有一条数据,让current--
      // 💕 一定一定要注意判断的时机,必须是在删除成功之后,重新渲染之前.因为删除有可能是失败的
      // 💕 额外添加一个条件,必须得current>1,才能减
      if (this.tableData.length === 1 && this.current > 1) {
        this.current--
      }
      // 重新渲染
      this.initData()
    },
    handleCurrentChange (page) {
      console.log('Page changed')
      console.log(page)
      // 改变页码值
      this.current = page
      // 重新获取数据
      this.initData()
    },
    async openDrawer (drawerType, id) {
      // console.log(drawerType, id)
      this.isShow = true
      this.drawerType = drawerType
      // 除了添加面经之外,其余两种情况都需要获取面经详情
      if (drawerType !== 'add') {
        const res = await getArticle(id)
        // 回显
        // this.form.stem = res.data.stem
        // this.form.content = res.data.content
        // this.form.id = res.data.id
        // 上面的对象赋值太麻烦,使用展开运算符,讲一个对象整体赋值给另一个对象
        // 不使用 this.form=res.data,因为这样付的是地址,数据不安全
        this.form = { ...res.data }
      }
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.closeDrawer()
        })
        .catch(e => {
          console.log('用户点击了取消', e)
        })
    },
    // submit () {
    //   this.$refs.form.validate(async (valid) => {
    //     // 如果表单校验不通过,就返回
    //     if (!valid) return
    //     // 发请求
    //     await addArticle(this.form)
    //     // 提示用户
    //     this.$message.success('添加成功')
    //     // 关闭抽屉
    //     this.isShow = false
    //     // 回到第1页
    //     this.current = 1
    //     // 重新获取数据
    //     this.initData()
    //   })
    // }
    async submit () {
      // 1.校验
      await this.$refs.form.validate()

      // 下面的2.3.回到第1页是不同的逻辑,放到一起
      if (this.drawerType === 'add') {
        // 2.发请求
        await addArticle(this.form)
        // 3. 提示用户
        this.$message.success('添加成功')
        // 回到第1页
        this.current = 1
      }
      if (this.drawerType === 'edit') {
        // 💕 这里测试是否能多传数据,这个接口是不可以的
        // await updateArticle(this.form)
        // 💕 按需传递
        await updateArticle({ id: this.form.id, stem: this.form.stem, content: this.form.content })
        // 3. 提示用户
        this.$message.success('编辑成功')
      }

      // 4.关闭抽屉
      this.closeDrawer()
      // 重新渲染
      this.initData()
    },
    closeDrawer () {
      this.isShow = false
      // 1. 这里加一个判断
      // if (this.drawerType !== 'preview') this.$refs.form.resetFields()
      // 2.判断的另一种写法
      this.$refs.form && this.$refs.form.resetFields()
      // form数据对象也要重置一下,只保留双向绑定的数据
      this.form = {
        stem: '',
        content: ''
      }
    }

  },
  computed: {

  }

}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }

  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-between;
    color: #666;

    >i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.el-breadcrumb {
  margin-top: 10px;
}

.el-form {
  padding-right: 40px;
}

.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}

.el-rate {
  padding: 10px 0;
}

.article-preview {
  padding: 0 40px 40px 40px;

  >h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
